package com.tjl.yangxixis.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.LogUtils;
import com.lyp.adapters.CarDetailsAdapter;
import com.lyp.jsonbean.CarOrdersDetailsBean;
import com.lyp.jsonbean.CarReceBean;
import com.lyp.jsonbean.JLCarBean;
import com.lyp.jsonbean.LoginBean;
import com.tjl.yangxixis.R;
import com.tjl.yangxixis.api.YangxixiApi;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
 *
 * @author Administrator
 *  接单(车内)详情
 */
public class SingeDetailsActivity extends Activity implements View.OnClickListener{

	private JLCarBean.DataBean carBean;
	private RecyclerView mRecyclerView;
	private LinearLayoutManager mLinearLayoutManager;
	private CarDetailsAdapter mAdapter;//车内
	private List<CarOrdersDetailsBean.DataBean> mList = new ArrayList<>();
	private Button mRece;
	private LoginBean.DataBean bean;
	private ImageView mBack;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_singe_indoor);
		init();
	}

	private void init(){
		carBean = (JLCarBean.DataBean) getIntent().getExtras().get("databean");
		bean = (LoginBean.DataBean) getIntent().getExtras().get("bean");
		mRecyclerView= (RecyclerView) findViewById(R.id.cardetails_recycler);
		mLinearLayoutManager = new LinearLayoutManager(this);
		mRecyclerView.setLayoutManager(mLinearLayoutManager);
		mAdapter = new CarDetailsAdapter(mList);
		mRecyclerView.setAdapter(mAdapter);

		Cardetail(carBean.getO_id());

		//车内接单按钮
		mRece = (Button) findViewById(R.id.btn_order_receiving);
		mBack = (ImageView) findViewById(R.id.iv_cardetails_back);
		mRece.setOnClickListener(this);
		mBack.setOnClickListener(this);
	}

	//车内接单
	public void Cardetail(String o_id){
		Retrofit retrofit = new Retrofit.Builder()
				.baseUrl(YangxixiApi.APP_URL)
				.addConverterFactory(GsonConverterFactory.create())
				.build();

		YangxixiApi api = retrofit.create(YangxixiApi.class);
		Call<CarOrdersDetailsBean> call = api.getCarOrders(o_id);
		call.enqueue(new Callback<CarOrdersDetailsBean>() {
			@Override
			public void onResponse(Call<CarOrdersDetailsBean> call, final Response<CarOrdersDetailsBean> response) {
				if (response.body().getResult() == 1 ) {
					SingeDetailsActivity.this.runOnUiThread(new Runnable() {
						@Override
						public void run() {
							mList.addAll(response.body().getData());
							mAdapter.notifyDataSetChanged();
						}
					});
				}
			}
			@Override
			public void onFailure(Call<CarOrdersDetailsBean> call, Throwable t) {
				LogUtils.i("tag","失败"+t.getMessage());
			}
		});
	}

	//车内接单按钮
	public void carRece(String o_id,String c_id) throws IOException {
		Retrofit retrofit = new Retrofit.Builder()
				.baseUrl(YangxixiApi.APP_URL)
				.addConverterFactory(GsonConverterFactory.create())
				.build();

		YangxixiApi api = retrofit.create(YangxixiApi.class);
		Call<CarReceBean> call = api.getCarRece(o_id,c_id);
		call.enqueue(new Callback<CarReceBean>() {
			@Override
			public void onResponse(Call<CarReceBean> call, final Response<CarReceBean> response) {
				if (response.body().getResult() == 1 ) {
					SingeDetailsActivity.this.runOnUiThread(new Runnable() {
						@Override
						public void run() {
							new MaterialDialog.Builder(SingeDetailsActivity.this)
									.title("接单成功")
									.content("快去未分配任务进行分配!")
									.positiveText("确定")
									.canceledOnTouchOutside(false)
									.cancelable(false)
									.onPositive(new MaterialDialog.SingleButtonCallback(){
										@Override
										public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
											setResult(RESULT_OK);
											finish();
										}
									})
									.show();
							mAdapter.notifyDataSetChanged();
						}
					});
				}
				else {
					Toast.makeText(SingeDetailsActivity.this,response.body().getMessage(),Toast.LENGTH_SHORT).show();
				}
			}
			@Override
			public void onFailure(Call<CarReceBean> call, Throwable t) {
				LogUtils.i("tag","失败"+t.getMessage());
			}
		});
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.btn_order_receiving:
				try {
					carRece(carBean.getO_id(),bean.getC_id());
				} catch (IOException e) {
					e.printStackTrace();
				}
				break;
			case R.id.iv_cardetails_back:
				finish();
				break;
		}
	}
}
